package com.gxuwz.medical.domain.medical;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.gxuwz.medical.database.DbUtil;
import com.gxuwz.medical.exception.UserNotFoundException;

/**
 * 医疗机构管理类
 * 
 * @author 演示
 * 
 */
public class Medical {

	private String medicalcode; // 医疗机构代码
	private String organizecode; // 组织机构代码
	private String medicalname; // 医疗机构名称
	private String diqucode; // 地区编码
	private String areacode; // 行政区域编码
	private String grade; // 级别
	private String type; // 卫生机构类别

	public String getMedicalcode() {
		return medicalcode;
	}

	public void setMedicalcode(String medicalcode) {
		this.medicalcode = medicalcode;
	}

	public String getOrganizecode() {
		return organizecode;
	}

	public void setOrganizecode(String organizecode) {
		this.organizecode = organizecode;
	}

	public String getMedicalname() {
		return medicalname;
	}

	public void setMedicalname(String medicalname) {
		this.medicalname = medicalname;
	}

	public String getDiqucode() {
		return diqucode;
	}

	public void setDiqucode(String diqucode) {
		this.diqucode = diqucode;
	}

	public String getAreacode() {
		return areacode;
	}

	public void setAreacode(String areacode) {
		this.areacode = areacode;
	}

	public String getGrade() {
		return grade;
	}

	public void setGrade(String grade) {
		this.grade = grade;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}
	
	public Medical(){
	}
	
	public Medical(String medicalcode)throws UserNotFoundException {

		load(medicalcode);
	}

	public void addMedical(String medicalcode, String organizecode,
			String medicalname, String diqucode, String areacode, String grade,
			String type) throws SQLException {
		Connection conn = null;
		try {
			this.medicalcode = medicalcode;
			this.organizecode = organizecode;
			this.medicalname = medicalname;
			this.diqucode = diqucode;
			this.areacode = areacode;
			this.grade = grade;
			this.type = type;
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			// 1:保存用户信息
			saveToDB(conn);
			// 提交事务
			conn.commit();

		} catch (SQLException e) {
			conn.rollback();
			throw e;
		} finally {
			DbUtil.close(conn);
		}
	}

	protected void saveToDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {

			StringBuffer sqlBuff = new StringBuffer();
			sqlBuff.append("insert into t_medical(medicalcode, organizecode, medicalname,diqucode,areacode,grade,type)");
			sqlBuff.append("values(?,?,?,?,?,?,?)");
			String sql = sqlBuff.toString();
			pstmt = conn.prepareStatement(sql);
			// 依次设置动态参数的值，注意顺序
			int index = 1;
			pstmt.setString(index++, this.medicalcode);// 行政区域编码
			pstmt.setString(index++, this.organizecode);
			pstmt.setString(index++, this.medicalname);
			pstmt.setString(index++, this.diqucode);
			pstmt.setString(index++, this.areacode);
			pstmt.setString(index++, this.grade);
			pstmt.setString(index++, this.type);
			// 以executeUpdate()执行SQL
			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw e;
		} finally {
			// 只关闭PrepareStatement资源
			if (pstmt != null) {
				pstmt.close();
			}

		}
	}
	
	public void delMedical(String medicalcode)throws SQLException{
		this.medicalcode = medicalcode;
		Connection conn = null;
		try{
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			deleteFromDB(conn);
			conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	
	private void deleteFromDB(Connection conn)throws SQLException{
		PreparedStatement pstmt = null;
		try{
			StringBuffer sqlBuffer = new StringBuffer("delete from t_medical where medicalcode=?");
			pstmt=conn.prepareStatement(sqlBuffer.toString());
			pstmt.setString(1, this.medicalcode);
			pstmt.executeUpdate();
		}catch(SQLException e){
			throw new SQLException("删除t_medical表时出错！");
		}finally{
			DbUtil.close(pstmt);
		}
	}
	
	public void editChronic(String medicalcode, String organizecode,
			String medicalname, String diqucode, String areacode, String grade,
			String type)throws SQLException{
		this.medicalcode = medicalcode;
		this.organizecode = organizecode;
		this.medicalname = medicalname;
		this.diqucode = diqucode;
		this.areacode = areacode;
		this.grade = grade;
		this.type = type;
		Connection conn = null;
		try{
			conn = DbUtil.getConn();
			conn.setAutoCommit(false);
			editFromDB(conn);
			conn.commit();
		}catch(SQLException e){
			conn.rollback();
			throw e;
		}finally{
			DbUtil.close(conn);
		}
	}
	
	private void editFromDB(Connection conn) throws SQLException {
		PreparedStatement pstmt = null;
		try {
			StringBuffer sqlBuff = new StringBuffer(
					"update t_medical t set t.organizecode=?,t.medicalname=?,t.diqucode=?,t.areacode=?,t.grade=?,t.type=? where t.medicalcode=?");
			pstmt = conn.prepareStatement(sqlBuff.toString());
			pstmt.setString(1, this.organizecode);
			pstmt.setString(2, this.medicalname);
			pstmt.setString(3, this.diqucode);
			pstmt.setString(4, this.areacode);
			pstmt.setString(5, this.grade);
			pstmt.setString(6, this.type);
			pstmt.setString(7, this.medicalcode);

			pstmt.executeUpdate();
		} catch (SQLException e) {
			throw new SQLException("Failed to update t_medical  !", e);
		} finally {
			DbUtil.close(pstmt);
		}
	}
	
	/**
	 * 
	 * @param id
	 * @throws Exception
	 */
	private void load(String medicalcode) throws UserNotFoundException {
		
		Connection conn = null;
		PreparedStatement pstmt = null;
		ResultSet rs = null;
		try {
			conn = DbUtil.getConn();
			pstmt = conn
					.prepareStatement("select * from t_medical where medicalcode=?");
			int index = 1;
			pstmt.setString(index, medicalcode);
			rs = pstmt.executeQuery();
			if (rs != null && rs.next()) {
				this.medicalcode = rs.getString("medicalcode");
				this.organizecode = rs.getString("organizecode");
				this.medicalname = rs.getString("medicalname");
				this.diqucode = rs.getString("diqucode");
				this.areacode = rs.getString("areacode");
				this.grade = rs.getString("grade");
				this.type = rs.getString("type");
			}

		} catch (SQLException e) {
			throw new UserNotFoundException("Medical with id " + medicalcode
					+ " could not be loaded from the database.");
		} finally {
			DbUtil.close(rs, pstmt, conn);
		}
	}

}
